<?php
/**
 * Created by PhpStorm.
 * User: Liuhongchuan
 * Date: 2018/4/28
 * Time: 10:05
 * 临时借记卡管理
 */
namespace app\operation\controller;

use app\common\controller\OperationBase;

class BorrowCard extends OperationBase {

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 临时卡
     */
    public function index()
    {
        $auth_group = sp_get_auth_menu_3("20031000");
        $shops = db("shop")->where(array("parent_id" => array("neq", 0)))->order("parent_id desc, shop_name desc")->column("id, shop_name");
        $this->assign("shops", $shops);
        $this->assign("menus", $auth_group);
        $this->assign("now", date("Y-m-d"));
        echo $this->fetch();
    }

    /**
     * 临时卡数据
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function ajaxIndex() {
        $card_no = input("card_no/d");
        $shop_id = input("shop_id/d");
        $draw = input("draw",0) + 1;//请求时间

        $where = array();
        if (!empty($card_no)) {
            $where["bc.card_no"] = array("like", "%{$card_no}%");
        }
        if (!empty($shop_id)) {
            $where["bc.shop_id"] = array("eq", $shop_id);
        }
        $total = db("borrow_card")->alias("bc")->where($where)->count(1);
        $list = db("borrow_card")->alias("bc")
            ->join("m_shop s", "s.id = bc.shop_id", "left")
            ->field("bc.*, s.shop_name")
            ->where($where)
            ->order($this->get_order())
            ->limit($this->get_limit())
            ->select();

        $data = array('pageData' => $list, 'total' => $total, "draw" => $draw);
        $this->ajax($data);
    }
}